<?php 
Class wxruleController extends appController
{
	/**
	 * 显示微信规则列表
	 */
	public function index()
	{
	   	$page = yf_empty(intval($_REQUEST['page']), 1, true);
		$menu_list = Model('wxrule')->get_rule_list($page, 10);
		$this->assign('menu_list',$menu_list['data']);
		$this->assign('menu_page',$menu_list['pagelink']);
		$this->display('rule_list.html');
	}
	
	/**
	 * 保存微信的设置
	 */
	public function save()
	{
		$this->admin_priv('set_weixin');
		$json['token'] = yf_empty($_POST['token'], '', true);
		$json['appid'] = yf_empty($_POST['appid'], '', true);
		$json['appsecret'] = yf_empty($_POST['appsecret'], '', true);
		
		$data = array('error' => 1, 'content' => Lang('INVALID_OPERATION'));
		$path = ROOT_PATH."/Qiaqia/Json/weixin.json";
		$fp = fopen($path, "w");
		fwrite($fp, json_encode($json));
		fclose($fp);
	  	$data['error'] = 0;
	  	$data['content'] = Lang('VALID_OPERATION');
		$this->showMsg($data);
	}
	
	/**
	 * 保存微信的设置
	 */
	public function saveconfig()
	{
	    $this->admin_priv('set_weixin');
	    $info['wxareply'] = yf_empty(intval($_POST['wxareply']), '', true);
	    $info['wxdreply'] = yf_empty(intval($_POST['wxdreply']), '', true);
	    $data = array('error' => 1, 'content' => Lang('INVALID_OPERATION'));
	    
	    if(Model('wxrule')->saveWeixin($info))
	    {
	        $data['error'] = 0;
	        $data['content'] = Lang('VALID_OPERATION');
	    }
	    $this->showMsg($data);
	}
	
	/**
	 * 显示微信设置页面
	 */
	public function weixin()
	{
	    $setting = Model()->weixin_config();
	    $this->assign('setting', $setting);
		$this->display('weixin.html');
	}
	
	/**
	 * 显示微信回复设置
	 */
	public function config()
	{
		$menu_list = Model('wxrule')->get_rule_list();
		$setting = Model('wxrule')->getWeixin();
		$this->assign('rule_list', $menu_list);
		$this->assign('setting', $setting);
        $this->display("config.html");	    
	}
	
	/**
	 * 显示添加规则页面
	 */
	public function add_rule()
	{
		$this->admin_priv('add_rule', 2);
		$this->assign('parent_list', Model('menu')->get_cat_list());
		$art_list = Model('article')->get_all_art();
		$this->assign('art_list', $art_list);
		$this->assign('action', 'create_rule');
		$this->display('rule.html');
	}
	
	/**
	 * 插入规则信息
	 */
	public function create_rule()
	{
		$this->admin_priv('add_rule');
		$img_arr = array();
		$i = 0;
		foreach($_POST as $k => $v)
		{
			$i++;
			if(strstr($k, 'field'))
			{
				$img_arr[$i]['title'] = $v[0];
				$img_arr[$i]['description'] = $v[1];
				$img_arr[$i]['url'] = $v[2];
				$img_arr[$i]['thumb'] = $v[3];
			}
		}
		
		$info['name'] = yf_empty($_POST['name'], '标题不能为空');
		$info['createtime'] = yf_time();
	    $info['type'] = yf_empty($_POST['type'], 1, true);
	    $keyword = yf_empty($_POST['keyword'], '', true);
	
		$data = array('error' => 0, 'content' => '');
		
		if($_POST['type']==1)
		{
		  	$content = yf_empty($_POST['content'], '', true);
		}
		else 
		{
			$content = $img_arr;
		}
		
		if(!Model('wxkey')->check_key($keyword) && $keyword != '')
		{
			$data['error'] = 1;
			$data['content'] = '关键词已经存在了';
			$this->showMsg($data);
		}
		
		$result = Model('wxrule')->insert_rule($info, $content);
		  //快速绑定关键词
		if($result['rule_id'] > 0 && $keyword!='')
		{
			$temp = array('keyword' => $keyword, 'rid' => $result['rule_id'], 'type'=> '0', 'status' => '1');
		   	Model('wxkey')->create_key($temp);
		}
		   
		if($result['error'] == 0)
		{
			$data['error'] = 0;
		   	$data['content'] = Lang('SUCCESS_ADD');
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	} 
	
	/**
	 * 删除微信规则
	 */
	public function del()
	{
		$this->admin_priv('del_rule');
		$id = yf_empty($_REQUEST['id'], '', true);
		if(is_array($id))
		{
			foreach($id as $k => $v)
			{
				$result = Model('wxrule')->del_rule($v);
			}
		}
		else
		{
			$result = Model('wxrule')->del_rule($id);
		}
		$this->showMsg($result);
	}
	
	/**
	 * 显示规则编辑页面
	 */
	public function edit_rule()
	{
		$this->admin_priv('edit_rule', 2);
		$id = yf_empty(intval($_GET['id']), '', true);
		$menu_info = Model('wxrule')->get_rule_info($id);
		$art_list = Model('article')->get_all_art();
		$this->assign('art_list', $art_list);
		$this->assign('rule', $menu_info);
		$this->assign('action', 'update_rule');
		$this->display('rule.html');
	}
	/*
	public function delimg()
	{
		$id = yf_empty($_REQUEST['id'], '', true);
		$res = Model('wxrule')->del(array('table'=>'wxrule', 'id'=>$id));
		$result = array('error' => 1, 'content' => Lang('INVALID_OPERATION'));
		if($res)
		{
			$result['error']=0;
		}
		$this->showMsg($result);
	}
	*/
	/**
	 * 更新规则数据
	 */
	public function update_rule()
	{
		$this->admin_priv('edit_rule');
		$img_arr = array();
		$i = 0;	
		foreach($_POST as $k => $v)
		{
			$i++;
			if(strstr($k, 'field'))
			{
				$img_arr[$i]['title'] = $v[0];
				$img_arr[$i]['description'] = $v[1];
				$img_arr[$i]['url'] = $v[2];
				$img_arr[$i]['thumb'] = $v[3];
				$img_arr[$i]['id'] = $v[4];
			}
		}
	
		$id = yf_empty(intval($_POST['id']), '', true);
		$info['name'] = yf_empty($_POST['name'], '标题不能为空');
		$info['createtime'] = yf_time();
		//$info['status'] = isset($_POST['is_show'])?intval($_POST['is_show']):0;
	
		$data = array('error' => 0, 'content' => '');
		
		if($_POST['type'] == 1)
		{
			$content = yf_empty($_POST['content'], '', true);
		}
		else
		{
			$content = $img_arr;
		}
		
		$result = Model('wxrule')->update_rule($info, $content, $id);
		if($result)
		{
			$data['content']=Lang('SUCCESS_EDIT');
			$data['url']="index.php?g=admin&m=wxrule&a=index";
		}
		else
		{
			$data['error']=1;
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	}
	
}